<?php

namespace App\Controllers\Member;

use App\Models\Tag\Tags;
use App\Models\User\User;
use App\Libs\Views\Json;
use App\Models\Member\MemOrder;
/**

 *
 *  获取用户详情；
 *
 *
 */

class GetMemberDetail extends \App\Libs\Base\BaseAction
{
    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = FALSE;
    }


    public function execute()
    {

        if(!$this->request->hasQuery('user_id')){
            return Json::view(1,'你要查谁的信息？',[]);
        }
        $userid = $this->request->getQuery('user_id');
        $conditions = [
            'id=?1',
            'bind' => [1=>$userid],
           'columns' =>'id,username,phone,avatar,email,vip,balance,address,total_time,week_time,total_credit,card_sum',
        ];
        $matchUser = User::findAll($conditions)->toArray();
        if(empty($matchUser)){
            return Json::view(1,'没有此用户',[]);
        }
        foreach ($matchUser as $key => $val){
            $userIDArray[] = $val['id'];
        }

        $matchMemOrder = MemOrder::findAll([
            'uid in ({ddd:array}) and unix_timestamp(end_time)>?1 and status=1',
            'columns' => 'id,uid,cate,start_time,end_time',
            'bind' => ['ddd' => $userIDArray,1=>time()]
        ]);

        //计算范围，计算时间；
        $openMemberLog = [];
        if($matchMemOrder->valid()){
            $matchMemOrder->rewind();
            while($matchMemOrder->valid()){
                $cur = $matchMemOrder->current();
                $openMemberLog[$cur->uid][$cur->cate][] = $cur->toArray(['']);
                $matchMemOrder->next();
            }
            //计算开通会员的人的每个课程的时间；
            //无奈使用嵌套循环;
            $formatMemberTime = [];
            foreach ($openMemberLog as $uidKey => $uAllCate){
                    //每个用户的循环
                    foreach ($openMemberLog[$uidKey] as $singleCateKey => $singlCeateOrder ){
                        //每个用户的每门课的循环
                        foreach ($openMemberLog[$uidKey][$singleCateKey] as $eachOrderKey => $eachOrder){
                           if(!isset($formatMemberTime[$uidKey][$singleCateKey])){
                               $formatMemberTime[$uidKey][$singleCateKey]['start_time'] = time();
                               $formatMemberTime[$uidKey][$singleCateKey]['end_time'] = time();
                           }
                           if(strtotime($eachOrder['start_time']) <= $formatMemberTime[$uidKey][$singleCateKey]['start_time']){
                               $formatMemberTime[$uidKey][$singleCateKey]['start_time'] = $eachOrder['start_time'];
                           }
                           if(strtotime($eachOrder['end_time']) >= $formatMemberTime[$uidKey][$singleCateKey]['end_time']){
                               $formatMemberTime[$uidKey][$singleCateKey]['end_time'] = $eachOrder['end_time'];
                           }
                        }
                    }
            }
        }else{
            $formatMemberTime = [];
        }

        //寻找课程代号；
        $allTags = Tags::findAll([
            'tag_type=9',
            'columns' => 'id,tag_name,data'
        ]);
        $courseData = [];
        $allTags->rewind();
        while($allTags->valid()){
            $curTag = $allTags->current();
            $coursecate = json_decode($curTag->data,true)['cate'];
            $courseData[$coursecate] = [
                'cateCode' => $coursecate,
                'cateName' => $curTag->tag_name
            ];
            $allTags->next();
        }
        foreach ($matchUser as $key => $val){
            if(isset($formatMemberTime[$matchUser[$key]['id']])){
                foreach ($formatMemberTime[$matchUser[$key]['id']] as $cateCode => $cateInfo){
                    $formatMemberTime[$matchUser[$key]['id']][$cateCode]['cateName'] =$courseData[$cateCode]['cateName'];
                }
                $matchUser[$key]['courseMemberTime'] = array_values($formatMemberTime[$matchUser[$key]['id']]);
            }else{
                $matchUser[$key]['courseMemberTime'] = [];
            }
            $matchUser[$key]['total_time'] = User::secToIntTime($matchUser[$key]['total_time']).'小时';
        }
        return Json::view(0,'ok',$matchUser[0]);
    }
}
